package Contacts;

import java.util.Scanner;

/**
 * Description:
 * 通讯录显示类：
 * 1.显示主菜单和与用户控制台交互信息
 * 2.对用户输入的信息作简单校验。
 *
 * Author:Jiali
 * Date:2022-08-23 15:39
 */
public class ContactsView {
    private Scanner scanner = new Scanner(System.in);
    private ContactsController ctrl = new ContactsController();

    /**
     * 菜单显示
     */
    public void menu(){
        System.out.println("+----------- ContactMenu  ------------+");
        System.out.println("|                                     |");
        System.out.println("|             1.Insert                |");
        System.out.println("|                                     |");
        System.out.println("|             2.Remove                |");
        System.out.println("|                                     |");
        System.out.println("|             3.Modify                |");
        System.out.println("|                                     |");
        System.out.println("|             4.Search                |");
        System.out.println("|                                     |");
        System.out.println("|             5.ShowAll               |");
        System.out.println("|                                     |");
        System.out.println("|             6.SortByName            |");
        System.out.println("|                                     |");
        System.out.println("|             7.ClearAll              |");
        System.out.println("|                                     |");
        System.out.println("|             0.Exit                  |");
        System.out.println("|                                     |");
        System.out.println("+------------ Version 1.0 ------------+");
    }

    /**
     * 主菜单入口
     */
    public void start(){
        int menuSelection = 0;

        do{
            menu();
            System.out.println("请选择：");
            menuSelection = scanner.nextInt();
            scanner.nextLine();

            switch (menuSelection){
                case 1:
                    insert();
                    break;
                case 2:
                    remove();
                    break;
                case 3:
                    modify();
                    break;
                case 4:
                    search();
                    break;
                case 5:
                    showAll();
                    break;
                case 6:
                    sort();
                    break;
                case 7:
                    clearAll();
                    break;
                case 0:
                    ctrl.saveData();
                    System.out.println("联系人信息已自动保存，正在退出通讯录。");
                    break;
                default:
                    System.out.println("选择错误!");
                    break;
            }
        }while(menuSelection != 0);
    }

    /**
     * 新增联系人
     */
    private void insert() {
        System.out.println("新增联系人...\n");
        System.out.print("姓名：");
        String name = scanner.nextLine();
        System.out.print("性别：");
        String gender = scanner.next();
        System.out.print("年龄：");
        int age = scanner.nextInt();
        scanner.nextLine();//过滤前一行末尾空格
        System.out.print("电话：");
        String phoneNumber = scanner.nextLine();
        System.out.print("备注：");
        String notes = scanner.nextLine();

        boolean ret = ctrl.insert(new Person(name,gender,age,phoneNumber,notes));
        if(ret){
            System.out.println("\"" + name + "\"已添加到通讯录。");
        }else{
            System.out.println("添加失败");
        }
    }

    /**
     * 删除联系人
     */
    private void remove(){
        if (ctrl.isEmpty()){
            System.out.println("联系人列表为空！");
            return;
        }
        System.out.println("请输入您要删除的联系人姓名：");
        String name = scanner.nextLine();
        if(ctrl.remove(name)){
            System.out.println("已删除联系人\"" + name + "\"。");
        }else{
            System.out.println("未找到此联系人。");
        }
    }

    /**
     * 修改联系人
     */
    private void modify(){
        if (ctrl.isEmpty()){
            System.out.println("联系人列表为空！");
            return;
        }
        System.out.println("请输入您要修改的联系人姓名：");
        String name = scanner.nextLine();
        int index = ctrl.queryByName(name);
        if(index != -1){
            Person person = ctrl.queryOne(index);
            System.out.println("联系人信息");
            System.out.println("姓名:" + person.getName());
            System.out.println("性别:" + person.getGender());
            System.out.println("年龄:" + person.getAge());
            System.out.println("电话:" + person.getPhoneNumber());
            System.out.println("备注:" + person.getNotes());

            System.out.println("\n修改各项信息:");
            System.out.print("姓名：");
            String changedName = scanner.nextLine();
            System.out.print("性别：");
            String gender = scanner.nextLine();
            System.out.print("年龄：");
            int age = scanner.nextInt();
            scanner.nextLine();//过滤前一行末尾空格
            System.out.print("电话：");
            String phoneNumber = scanner.nextLine();
            System.out.print("备注：");
            String notes = scanner.nextLine();

            ctrl.modify(index,new Person(changedName,gender,age,phoneNumber,notes));
            System.out.println("修改成功！");
        }else{
            System.out.println("未找到此联系人。");
        }
    }

    /**
     * 查找联系人
     */
    private void search(){
        if (ctrl.isEmpty()){
            System.out.println("联系人列表为空！");
            return;
        }
        System.out.println("请输入您要查找的联系人姓名：");
        String name = scanner.nextLine();
        int index = ctrl.queryByName(name);
        if(index != -1){
            System.out.println(ctrl.queryOne(index));
        }else{
            System.out.println("未找到此联系人。");
        }
    }

    /**
     * 显示所有联系人
     */
    private void showAll(){
        Person[] people = ctrl.queryAll();
        if (people != null){
            for (Person p : people){
                System.out.println(p);
            }
        }else{
            System.out.println("联系人列表为空！");
        }
    }

    /**
     * 按姓名排序联系人列表
     */
    private void sort(){
        if (ctrl.isEmpty()){
            System.out.println("联系人列表为空！");
            return;
        }
        ctrl.sortByName();
        System.out.println("排序完毕！");
    }

    /**
     * 清空联系人列表
     */
    private void clearAll(){
        if (ctrl.isEmpty()){
            System.out.println("联系人列表已为空！");
            return;
        }
        System.out.println("您确定要清空通讯录吗？此操作不可逆哦！");
        System.out.println("确认：Yes|yes，取消：No|no");
        String isClear = scanner.nextLine();
        if("Yes".equalsIgnoreCase(isClear)){
            ctrl.clearAll();
            System.out.println("清空完毕！");
        }
    }
}
